woyouhuo = {
	baseUrl: "",
	currencySign: "￥",
	currencyUnit: "元",
	priceScale: "2",
	priceRoundType: "roundHalfUp"
};

//货币格式化 88.88
function priceFormat(price) {
	price = setScale(price, woyouhuo.priceScale, woyouhuo.priceRoundType);
	return price;
}

//浮点数加法运算
function floatAdd(arg1, arg2) {
	var r1, r2, m;
	try{
		r1 = arg1.toString().split(".")[1].length;
	} catch(e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch(e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return (arg1 * m + arg2 * m) / m;
}

// 浮点数减法运算
function floatSub(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch(e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch(e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2));
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

// 浮点数乘法运算
function floatMul(arg1, arg2) {
	var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length;
	} catch(e) {}
	try {
		m += s2.split(".")[1].length;
	} catch(e) {}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

// 浮点数除法运算
function floatDiv(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;    
	try {
		t1 = arg1.toString().split(".")[1].length;
	} catch(e) {}
	try {
		t2 = arg2.toString().split(".")[1].length;
	} catch(e) {}
	with(Math) {
		r1 = Number(arg1.toString().replace(".", ""));
		r2 = Number(arg2.toString().replace(".", ""));
		return (r1 / r2) * pow(10, t2 - t1);
	}
}

// 设置数值精度
function setScale(value, scale, roundingMode) {
	if (roundingMode.toLowerCase() == "roundhalfup") {
		return (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else if (roundingMode.toLowerCase() == "roundup") {
		return (Math.ceil(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else {
		return (Math.floor(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	}
}


/**
 * 
 * @param url   请求地址
 * @param data   提交的数据
 * @param callBack success回调函数
 */
function G_JSON_POST(url,data,callBack,start,over) {
	var _Data = [];
	$.ajax({
		url: woyouhuo.baseUrl+url,
		data: data,
		type: 'POST',
		dataType: "json",
		beforeSend:function() {if(start)start(data);},
		success: function(data) {
			_Data = eval(data);
			callBack(data);
		},
		error:function(x){
			if(403==x.status){
				if(isWeiXin()){
					window.location.href=woyouhuo.baseUrl+"/ddjLoginAuth/goLogin";
				}else{
					window.location.href=woyouhuo.baseUrl+"/ddjLoginAuth/goLogin";
				}
			}
		},
		complete:function (XHR,TS) {
			if(over)over(_Data);
			XHR = null;
			
		}
	});
}

function G_JSON_GET(url,data,callBack,start,over) {
	var _Data = [];
	$.ajax({
		url: woyouhuo.baseUrl+url,
		data: data,
		type: 'GET',
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		beforeSend:function() {if(start)start(data);},
		success: function(data) {
			_Data = eval(data);
			callBack(data);
		},
		error:function(x){
			if(403==x.status){
				if(isWeiXin()){
					window.location.href=woyouhuo.baseUrl+"/ddjLoginAuth/goLogin";
				}else{
					window.location.href=woyouhuo.baseUrl+"/ddjLoginAuth/goLogin";
				}
				
			}
		},
		complete:function (XHR,TS) {
			if(over)over(_Data);
			XHR = null; 
		}
	});
}





/**
 * 判断是否是微信浏览器
 * @returns {Boolean}
 */
function isWeiXin() {
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true;
	} else {
		return false;
	}
}



function tipDialog(msg){
	
	  var $wrap = $('<div class="tip-box">'
			  +'<div class="tip-con">'
			   +msg+'</div></div>');
	  $("body").append($wrap);

	  $wrap.fadeIn(1200);

	  $wrap.click(function(){
	    $wrap.fadeOut(1200).remove();
	  })

	  setTimeout(function(){
	    $wrap.fadeOut(1000).remove();
	    },1300);
}

function getGoodsLabels(goodslabel){
	if("NEW"==goodslabel){
		return "tip-new";
	}else if("XIANSHI"==goodslabel){
		return "tip-timeAct timeAct";
	}else if("ZSONG"==goodslabel){
		return "tip-give";
	}else if("HOT"==goodslabel){
		return "tip-hot";
	}else if("TEJIA"==goodslabel){
		return "tip-tj";
	}else{
	    return "";
	}
}


function checkMobilePhone(v){
    var reg = /^(1[1|2|3|4|5|6|7|8|9])[\d]{9}$/;   //验证手机号码
	if(!reg.test(v)){
		return false;
	}
	return true;
}



$().ready( function() {
	//加入购物车
	$.addToCart = function(id,quantity){
		if($.trim(id) == ""){
			alert("请选择您要购买的商品");
			return false;
		}

		if (!/^[0-9]*[1-9][0-9]*$/.test($.trim(quantity))) {
			alert("商品品数量必须为正整数!");
			return false;
		}


		G_JSON_POST('/shop/cartItem/addCart',{"pId": id, "quantity": quantity},function(data){
					if (data.status == "success") {
						cartDialog();
					} else {
						alert(data.message)
					}
		});

		/**
		$.ajax({
			url: woyouhuo.baseUrl + "/shop/cartItem/addCart",
			data: {"pId": id, "quantity": quantity},
			type: "POST",
			dataType: "json",
			cache: false,
			success: function(data) {
				if (data.status == "success") {
					cartDialog();
				} else {
					alert(data.message)
				}
			},error:function(x){
				if(403==x.status){
					window.location.href="/alipay_auth_error";
				}
				console.log(x.status);
			}
		});
		*/
	}

	function cartDialog(){
		  var $wrap = $('<div class="maskbox-suc" id="cartTip">'
						   +'<P class="ui-icon icon-suc-basket"></P>'
						   +'<p> 加入购物车成功</P></div>');
		  $("#maskbox").append($wrap);

		  $wrap.fadeIn(1200);

		  $wrap.click(function(){
			$wrap.fadeOut(1200).remove();
		  })

		  setTimeout(function(){
			$wrap.fadeOut(1000).remove();
			$("#maskbox").fadeOut(1000);
			},1300);
	}
})



